<template lang="pug">
  .page
    .AccountsReceivableManage
      .search-form.mb-10.pl-10.pr-10(v-if="hasFind")
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="70")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.value"
                  :key="index"
                ) {{ c_item.label }}
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      .pl-20.pr-20.bg-white
        Table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="demoTable" @on-selection-change="handleOnSelectChange")
          .clearfix.font-16(slot="header")
            .table-title-lefttxt.pull-left
              |合计
              span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
              |条
            .pull-right.table-title-btns
              Button.mr-10(v-if="hasImport" type="info" @click="handleShowBatchImportModal") 导入
              Button(v-if="hasDelete" type="warning" :disabled="!selectedId.length > 0" @click="isDeleteModal") 删除
          .clearfix.pl-20.pr-20(slot="footer")
            Page(
              :total="tableComone.totalRows"
              @on-change="onChangePage"
              @on-page-size-change="onpageSizeChange"
              show-sizer
              show-total
              placement="top"
              prev-text="上一页"
              next-text="下一页"
              :page-size-opts="[10, 20, 30, 50, 100]"
            )

    //- 批量导入弹窗
    BatchImportModal(ref="BatchImportModal" @query="searchFn")
</template>

<script>
import { Common } from '@/utils/common.js'
import { YearReviewTimeApi } from '@/api/YearReviewTime.api.js'
import { baseUploadThead } from '../tableHead.js'
import _cloneDeep from 'clone-deep'
import BatchImportModal from './modal/batchImportModal.vue'

export default {
  name: '', // 基数上传时间节点

  components: {
    BatchImportModal
  },

  data () {
    return {
      thead: _cloneDeep(baseUploadThead),
      tableData: [],
      selectedId: [], // 勾选的id集合,array格式
      selectedArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1,
        totalRows: 0,
        pageSize: 10
      },
      formData: {
        securityType: '',
        securityNO: ''
      },
      formItem: [
        { type: 'select', label: '参保类型：', placeholder: '请选择参保类型', prop: 'securityType', model: 'securityType', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-9 ivu-col-span-xxl-6', options: 'SecurityType' },
        { type: 'input', label: '参保编号：', placeholder: '请输入参保编号', prop: 'securityNO', model: 'securityNO', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-9 ivu-col-span-xxl-6' },
        { type: 'button', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-3 ivu-col-span-xxl-3' }
      ],
      rules: {},
      arrListDown: {
        SecurityType: []
      }
    }
  },
  computed: {
    actionArr () {
      let arr = this.$store.state.user.actionAccess.map(item => {
        return item.code
      })
      return arr
    },
    // 查询
    hasFind () {
      return this.actionArr.indexOf('Find') > -1
    },
    // 导入
    hasImport () {
      return this.actionArr.indexOf('Import') > -1
    },
    // 删除
    hasDelete () {
      return this.actionArr.indexOf('Delete') > -1
    }
  },

  mounted () {
    this.getEnum()
    this.query()
  },

  methods: {
    // 数据字典
    getEnum () {
      let groupName = 'YearAdjustment.SecurityType'
      Common.getEnum3(groupName, this.arrListDown)
      console.log('数据字典：', this.arrListDown)
    },

    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        SecurityType: this.formData.securityType,
        SecurityNO: this.formData.securityNO,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await YearReviewTimeApi.getList(params)
      console.log('获取列表res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
      } else {
        this.tableData = []
      }
      this.selectedId = []
      this.selectedArr = []
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedArr = value.map((item) => {
        return { id: item.id, name: item.invoiceItem }
      })
      this.selectedId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedArr)
      console.log('列表勾选数组2', this.selectedId)
    },

    // 批量导入-显示弹窗
    handleShowBatchImportModal () {
      this.$refs.BatchImportModal.show()
    },

    // 是否删除前询问
    isDeleteModal () {
      this.$Modal.confirm({
        title: '是否确认删除',
        onOk: () => {
          this.handleSureDelete()
        }
      })
    },

    // 确认删除
    async handleSureDelete () {
      console.log('确认删除', this.selectedId.join(','))
      let res = await YearReviewTimeApi.delete({ id: this.selectedId.join(',') })
      if (res.data.success) {
        this.$Message.success(res.data.message)
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.query()
    }
  } // methods end
}
</script>

<style lang="less" scoped>

</style>
